package com.study.scala

object WordCount {
  def main(args: Array[String]): Unit = {
    val lines = List("hello world hello java hello scala", "hello world hello java hello scala", "hello java hello scala")
    //获取单词
    val words = lines.flatMap(_.split(" "))
    println(words)
    //用元组来存储
    val tuples = words.map((_, 1))
    println(tuples);
    //通过key分组
    val grouped = tuples.groupBy(_._1)
    println(grouped);
    //计算单词数量
    val result = grouped.mapValues(_.size)
    //排序
    val sorted = result.toList.sorted.reverse
    println(sorted)
  }
}